 package com.example.farmer.Seller_Home;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.Adapter.MyPurchaseAdapter;
import com.example.farmer.Database.PurchasePublishDatabase;
import com.example.farmer.Entity.PurchasePost;
import com.example.farmer.R;

import java.util.List;

 public class ActivityPurchase extends AppCompatActivity {

     private PurchasePublishDatabase purchasePublishDatabase;
     private MyPurchaseAdapter adapter;
     private List<PurchasePost> purchasePostList;
     private RecyclerView recyclerView;
     private TextView textViewPostContent;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);

        textViewPostContent = findViewById(R.id.purchase_not_published);

        purchasePublishDatabase = PurchasePublishDatabase.getInstance(this);
        purchasePostList = purchasePublishDatabase.getPostsByPushType("求购");

        // 这里可以设置Adapter来显示列表数据
        recyclerView = findViewById(R.id.rv_purchase);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyPurchaseAdapter(purchasePostList, this);
        recyclerView.setAdapter(adapter);

        updateUiWithPosts();
    }

     private void updateUiWithPosts() {
         // 如果posts不为空，则通知适配器数据已更改
         if (purchasePostList != null && !purchasePostList.isEmpty()) {
             // 假设PostAdapter实现了相应的方法来更新数据
             adapter = (MyPurchaseAdapter) recyclerView.getAdapter();
             purchasePostList = purchasePublishDatabase.getPostsByPushType("求购");
             if (adapter != null) {
                 adapter.updatePosts(purchasePostList);
             }
         } else {
             textViewPostContent.setText("你还没有发布货品哦"); // 假设您有一个TextView来显示消息
         }
     }
 }
